package cn.itcast.algorithm.exercise;

import cn.itcast.algorithm.exercise.helpclass.ListNode;
import org.junit.Test;

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class ExerciseAB10 {
    @Test
    public void exerciseAB10Test() {
        ListNode head = new ListNode(0) ;
        ListNode data1 = new ListNode(1) ;
        ListNode data2 = new ListNode(2) ;
        ListNode data3 = new ListNode(3) ;
        head.next = data1 ;
        data1.next = data2 ;
        data2.next = data3 ;

        System.out.println(head);

        ListNode newHead = reverseList(head) ;

        System.out.println(newHead);
    }

    /**
     * 反转队列
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode end = head, now;
        while (end.next != null) {
            now = head;
            head = end.next;
            end.next = head.next;
            head.next = now;
        }

        return head;
    }
}
